package gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;


public class GridPanel extends JComponent
{
    private static final int GRID_GAP = 1;
    private static final int GRID_HEIGHT = 9;
    private static final int GRID_WIDTH = 9;
    private static final Color COLOR_UNLOCKED = Color.blue;
    private static final Color COLOR_LOCKED = Color.green;
    private static final Color COLOR_EMPTY_CELL = Color.black;
    private GridCellPanel[][] cells;
    
    public GridPanel(int [][] grid)
    {
    	init(grid);
    	setVisible(true);
    }
    
    public void init(int [][] grid)
    {
        setLayout(new GridLayout(GRID_WIDTH / 3, GRID_HEIGHT / 3));
        setBorder(BorderFactory.createLineBorder(Color.BLACK, GRID_GAP));
        setBackground(Color.WHITE);
        setOpaque(true);

        cells = new GridCellPanel[GRID_WIDTH][GRID_HEIGHT];
        
        for (int i = 0; i < 9; i++)
        {
        	for (int j = 0; j < 9; j++)
        	{
        		if (grid[i][j] != 0)
        			cells[i][j] = new GridCellPanel(grid[i][j], COLOR_LOCKED);
        		else
        			cells[i][j] = new GridCellPanel(0, COLOR_EMPTY_CELL);
        	}
        }
        drawCells();
    }
    
    /**
     * Update the grid with new value positions.
     * @param grid
     */
    public void updateGridCells(int [][] grid)
    {
    	for (int i = 0; i < 9; i++)
        {
        	for (int j = 0; j < 9; j++)
        	{
        		if (grid[i][j] != 0)
        		{
        			if (cells[i][j].getCellColor() != COLOR_LOCKED)
        				cells[i][j].setCellColor(COLOR_UNLOCKED);
        			cells[i][j].setCellValue(grid[i][j]);
        		}
        		else
        			cells[i][j] = new GridCellPanel(0, COLOR_EMPTY_CELL);
        	}
        }
    	drawCells();
    }
    
    /**
     * Place values into the cells.
     */
    public void drawCells()
    {
    	removeAll();
    	JPanel[] panels = new JPanel[9];
        int panelIndex;
        
        for (int x = 0; x < GRID_WIDTH; x++)
        {
            for (int y = 0; y < GRID_HEIGHT; y++)
            {
                panelIndex = ((int) Math.floor(y / 3) * 3) + (int) Math.floor(x / 3);
                
                if (null == panels[panelIndex])
                {
                    panels[panelIndex] = new JPanel();
                    panels[panelIndex].setLayout(new GridLayout(3, 3));
                    panels[panelIndex].setBorder(BorderFactory.createLineBorder(Color.BLACK, GRID_GAP));
                    add(panels[panelIndex]);
                }

                panels[panelIndex].add(cells[x][y]);
            }
        }
        repaint();
    }
}
